from selenium import webdriver  #用于操作浏览器
from selenium.webdriver.chrome.options import Options  #用于设置谷歌浏览器
from selenium.webdriver.chrome.service import Service  #用于管理驱动
from selenium.webdriver.common.by import By
import time
import threading

#多线程执行自动化——四开浏览
class A:
    def __init__(self,x,y):
        self.x=x
        self.y=y

    # 设置浏览器、启动浏览器
    def func1(self):
        q1 = Options()
        q1.add_argument('--no-sandbox')
        q1.add_experimental_option('detach', True)
        a1 = webdriver.Chrome(service=Service('chromedriver.exe'), options=q1)
        a1.set_window_position(self.x,self.y)
        a1.set_window_size(200,400)
        a1.implicitly_wait(30)
        a1.get('https://bahuyun.com/bdp/form/1335521405670260737')
        return a1

    #执行代码
    def func2(self):
        a1=self.func1()
        for x in range(3):
            time.sleep(2)
            for x in range(2):
                # 操作单选框
                a1.find_element(By.XPATH, '//*[@id="my-node"]/div[1]/div/div[2]/div/div/div[1]').click()
                time.sleep(2)
                a1.find_element(By.XPATH, '//*[@id="my-node"]/div[1]/div/div[2]/div/div/div[2]').click()
                time.sleep(2)
                a1.find_element(By.XPATH, '//*[@id="my-node"]/div[1]/div/div[2]/div/div/div[3]').click()
                # 操作多选框
                a1.find_element(By.XPATH, '//*[@id="my-node"]/div[2]/div/div[2]/div/div/div[1]/span').click()
                a1.find_element(By.XPATH, '//*[@id="my-node"]/div[2]/div/div[2]/div/div/div[2]/span').click()
                a1.find_element(By.XPATH, '//*[@id="my-node"]/div[2]/div/div[2]/div/div/div[3]/span').click()
                time.sleep(2)
                # 操作下拉元素
                a1.find_element(By.XPATH, '//*[@id="my-node"]/div[3]/div/div[2]/div/div/div/select/option[2]').click()
                # 操作评星
                a1.find_element(By.XPATH, '//*[@id="my-node"]/div[4]/div/div[2]/div/div[1]/div[2]/div[2]/i[1]').click()
                a1.find_element(By.XPATH, '//*[@id="my-node"]/div[4]/div/div[2]/div/div[2]/div[2]/div[5]/i[1]').click()
                # 操作日期
                a1.find_element(By.XPATH, '//*[@id="input--W2vbG04d963X3l_Xyz_w"]').send_keys("0020031024")
                # 提交表单
                a1.find_element(By.XPATH, '//*[@id="submit-button"]').click()
                time.sleep(3)
                # 点击再填一次
                a1.find_element(By.XPATH, '//*[@id="app"]/div/div/div[1]/div[2]/div[3]/button').click()

s1=A(0,0)
s2=A(400,0)
s3=A(0,300)
s4=A(400,300)

threading.Thread(target=s1.func2).start()
threading.Thread(target=s2.func2).start()
threading.Thread(target=s3.func2).start()
threading.Thread(target=s4.func2).start()





